import 'package:flutter/material.dart';
import 'routes.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // const MyApp({Key? key}) : super(key: key);
  // This widget is the root of your application.

  Map<String, Widget Function(BuildContext)> _generateRoute(context) {
    final routes = {
      '/chapter2/state': (BuildContext context) => const StatePage(),
      '/chapter2/get_state': (BuildContext context) =>
          const GetStateObjectRoute(),
      '/chapter2/counter': (BuildContext context) => const CounterPage(),
      '/chapter2/cupertino_route': (BuildContext context) =>
          const CupertinoTestRoute(),
      '/chapter2/route': (BuildContext context) => const RouterTestRoute(),
      '/chapter3/buttons': (BuildContext context) => const ButtonRoute(),
      '/chapter3/form': (BuildContext context) => const FormTestRoute(),
      '/chapter3/image_icon': (BuildContext context) => const ImageIconRoute(),
      '/chapter3/iconfont': (BuildContext context) => const IconFontRoute(),
      '/chapter3/progress': (BuildContext context) => const ProgressRoute(),
      '/chapter3/textfield': (BuildContext context) => const TextFieldRoute(),
      '/chapter3/switch_checkbox': (BuildContext context) =>
          const SwitchAndCheckBoxRoute(),
      '/chapter4/afterlayout': (BuildContext context) =>
          const AfterLayoutRoute(),
      '/chapter4/table': (BuildContext context) => const TableRoute(),
       '/chapter4/algin': (BuildContext context) => const AlignRoute(),
       '/chapter4/constraints': (BuildContext context) => const SizeConstraintsRoute(),
       '/chapter4/layoutbuilder': (BuildContext context) => const LayoutBuilderRoute(),
       '/chapter4/rowcolumn': (BuildContext context) => const RowColumnRoute(),
    };
    return routes;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: '测试',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(),
      initialRoute: '/',
      routes: _generateRoute(context),
    );
  }
}

/**
 * 知识点：
 * 1.函数返回一个widget的List，widget就是ListTile组件
 * 2.取得的是一个个map对象，所以要.toList把一个个对象组合成数组返回出来
 * 2，list的用法
 * 有一个List类型数组，对象map里面的第一个参数title是String类型，第二个参数routeName是String类型，
 * page['title']!是指定空类型
 * 3.路由跳转用Navigator.pushName(context,'路由路径')
 * 
*/
List<Widget> _generateRouteItem(
    BuildContext context, List<Map<String, String>> children) {
  return children.map<Widget>((page) {
    return ListTile(
      title: Text(page['title']!),
      trailing: const Icon(Icons.keyboard_arrow_right),
      onTap: () => Navigator.pushNamed(context, page['routeName']!),
    );
  }).toList();
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  List<Widget> _getRouteItem(context) {
    return _generateRouteItem(context, [
      {'title': 'counter计数器', 'routeName': '/chapter2/counter'},
      {'title': 'state管理', 'routeName': '/chapter2/state'},
      {'title': '获取state', 'routeName': '/chapter2/get_state'},
      {'title': '自定义navigatinbar', 'routeName': '/chapter2/cupertino_route'},
      {'title': '路由传参', 'routeName': '/chapter2/route'},
      {'title': '按钮', 'routeName': '/chapter3/buttons'},
      {'title': '表单', 'routeName': '/chapter3/form'},
      {'title': '图片和icon', 'routeName': '/chapter3/image_icon'},
      {'title': '字体图标', 'routeName': '/chapter3/iconfont'},
      {'title': '自定义进度条', 'routeName': '/chapter3/progress'},
      {'title': 'switch_checkbox', 'routeName': '/chapter3/switch_checkbox'},
      {'title': 'text输入框', "routeName": '/chapter3/textfield'},
      {'title': 'afterlayout生命周期', 'routeName': '/chapter4/afterlayout'},
      {'title': 'table表格', 'routeName': '/chapter4/table'},
      {'title':'align对齐方式','routeName':'/chapter4/algin'},
      {'title':'约束','routeName':'/chapter4/constraints'},
      {'title':'动态创建layout','routeName':'/chapter4/layoutbuilder'},
      {'title':'row-column布局','routeName':'/chapter4/rowcolumn'},
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('测试页面'),
      ),
      body: ListView(
        children: _getRouteItem(context),
      ),
    );
  }
}
